<?php 
class stringutils{
	
	public static function line($text,$linenum){
		
	}
	
	public static function html_remove_tag($text,$tagname){
		if($text && $tagname){
			
		}
	}
	public static function between($text,$before=null,$after=null,$charset='utf-8'){
		if($before==null || $before==''){
			$pos1 = 0;
		}else{
			$pos1 = mb_strpos($text,$before,0,$charset);
		}
		if($pos1===false){
			return "";
		}
		$pos1=$pos1+mb_strlen($before,$charset);
		
		if($after==null || $after==''){
			$pos2 = mb_strlen($text,$charset);
		}else{
			$pos2 = mb_strpos($text,$after, $pos1,$charset);
		}
		if($pos2===false){
			return "";
		}
		
		$match = mb_substr($text, $pos1,$pos2-$pos1,$charset);
		if($match){
			return $match;
		}else{
			return "";
		}
	}

	public static function betweens($text,$before=null,$after=null,$charset='utf-8'){
		$list = array();
		$offset = 0;
		while($offset<mb_strlen($text,$charset)){
			if($before==null || $before==''){
				$pos1 = 0;
			}else{
				$pos1 = mb_strpos($text,$before,$offset,$charset);
			}
			if($pos1===false){
				break;
			}
			$pos1=$pos1+mb_strlen($before,$charset);
			
			if($after==null || $after==''){
				$pos2 = mb_strlen($text,$charset);
			}else{
				$pos2 = mb_strpos($text,$after, $pos1,$charset);
			}
			if($pos2===false){
				break;
			}
			
			$match = mb_substr($text, $pos1,$pos2-$pos1,$charset);
			if($match){
				array_push($list, $match);
				if($after==null || $after==''){
					break;
				}else{
					$offset=$pos2+mb_strlen($after,$charset);
				}
			}else{
				break;
			}
		}
		return $list;
	}
	
	/**
	 * Return true if the $var has no value or the value is null or empty string or an empty array.
	 * @param  $mix
	 */
	public static function isEmpty($mix){
		if(!isset($mix)){
			return true;
		}
		if($mix==null || $mix==""){
			return true;
		}
		if(is_array($mix) && count($mix)==0){
			return true;
		}
		return false;
	}
	
	public static function tonumber($v){
		if($v==null || $v==''){
			return 0;
		}
		if(is_numeric($v)){
			return (float)$v;
		}
		return 0;
	}
	public static function isNotEmpty($mix){
		return !StringUtils::isEmpty($mix);
	}
	
	public static function endWith($str, $suffix){
		if(StringUtils::isEmpty($str) || StringUtils::isEmpty($suffix)){
			return false;
		}
		
		if(strlen($str)<strlen($suffix)){
			return false;
		}
		
		if(substr($str,strlen($str)-strlen($suffix),strlen($suffix))==$suffix){
			return true;
		}
		
		return false;
	}
	
	public static function startWith($str, $prefix){
		if(StringUtils::isEmpty($var) || StringUtils::isEmpty($prefix)){
			return false;
		}
		
		if(strlen($str)<strlen($prefix)){
			return false;
		}
		
		if(substr($str,0,strlen($prefix))==$prefix){
			return true;
		}
		
		return false;
	}

	public static function indexOf($str, $substr,$from=null){
		return strpos($str, $substr,$from);
	}
	public static function stringBetween($str,$before,$after=null){
		if(StringUtils::isEmpty($str) || StringUtils::isEmpty($before)){
			return null;
		}
		
		if(StringUtils::isEmpty($after)){
			$after = $before;
		}
		
		$beforePos = StringUtils::indexOf($str, $before);
		
		if(!$beforePos || $beforePos==strlen($str)-strlen($before)){
			return null;
		}
		
		$from = $beforePos+strlen($before);
		
		$afterPos = StringUtils::indexOf($str,$after,$from);
		
		if(!$afterPos){
			return null;
		}
		if($afterPos==$from){
			return "";
		}
		$to = $afterPos;
		
		return substr($str, $from,$to-$from);
	}
	
	public static function split($str,$separator){
		return explode($separator,$str);
	}
	public static function date_now_to_int(){
		return time();
	}
	public static function date_int_to_array($dateint){
		return getdate($array);
	}
	public static function date_array_to_int($array){
		return mktime($array['hours'],$array['minutes'],$array['seconds'],$array['month'],$array['mday'],$array['year']);
	}
	public static function date_string_to_int($datestr){
		return strtotime($datestr);
	}
	
	public static function date_int_to_string($dateint,$format='Y-m-d H:i:s'){
		return date($format,$dateint);
	}
}
?>